package com.qf.openapi.es.consumer;

import com.qf.openapi.es.esvo.ApiTimeCostEsVo;
import com.qf.openapi.es.repository.TimeCostRepository;
import com.qf.qfsy.mqbean.ApiTimeCostMqBean;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class ApiTimeCostConsumer {

    @Autowired
    TimeCostRepository timeCostRepository;

    @RabbitListener(queues = "openapi-timecost-queue")
    public void getMsg(ApiTimeCostMqBean mqBean, Message message, Channel channel) throws IOException {
        log.info("接收到mqbean:{}",mqBean);

        // 把mqBean中的信息写入es索引中
        ApiTimeCostEsVo apiTimeCostEsVo = new ApiTimeCostEsVo();
        // 拷贝同名属性到新对象，第一个参数是有值的对象，第二参数是待赋值的对象
        BeanUtils.copyProperties(mqBean,apiTimeCostEsVo);

        timeCostRepository.save(apiTimeCostEsVo);
        // 手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
